<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------

namespace app\api\logic;

use \tpfcore\Core;
use think\Validate;
use tpfcore\web\Curl;
use \tpfcore\helpers\StringHelper;
class ProgramUser extends ApiBase
{
    public function login($data = [])
    {

        $validate = \think\Loader::validate($this->name);
        $validate_result = $validate->scene('login')->check($data);
        if (!$validate_result) {
            return [4003, $validate->getError(), []];
        }

        $appid=config("config.mimi_program")['config']['appid'];
        $secret=config("config.mimi_program")['config']['secret'];
        $js_code=$data['code'];

        $url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$js_code&grant_type=authorization_code";

        /*
        array(2) {
          ["session_key"]=>
          string(24) "uRhbD04shcgMSjygJxLHpA=="
          ["openid"]=>
          string(28) "oOTwQ5Xiz0v5TPmRfXLbukYMLf9w"
        }
        Array
        (
            [errcode] => 40125
            [errmsg] => invalid appsecret, view more at http://t.cn/RAEkdVq, hints: [ req_id: gECewqLnRa-p4Ev4 ]
        )
        */
        $result = Curl::get($url);
        $result = json_decode($result,true);

        if(!empty($result['errcode'])){
            return [40044, 'fail'];
        }else{
            $openid = $result['openid'];

            $listUser= Core::loadModel("User")->getOneObject(["openid"=>$openid],"id");

            $data['openid']=$openid;
            $data['headimg']=empty($data['headimg'])?"":$data['headimg'];
            $data['nickname']=empty($data['nickname'])?"会员昵称":$data['nickname'];
            $data['gender']=empty($data['gender'])?1:$data['gender'];
            $token = StringHelper::get_random_string(32);
            $data['token']=md5($token.time());

            if($listUser){  // 表示数据库里面已经有了
                $data['id']=$listUser['id'];
            }

            $res = Core::loadModel("User")->saveObject($data);


            if($res){
                return [0, 'success', ["code"=>$result["openid"],"expires_in"=>30*3600*24,"token"=>$data['token']]];        
            }else{
                return [40045, 'fail'];  
            }
            
        }
        
    }
    public function bd_login($data=[]){
        $validate = \think\Loader::validate($this->name);
        $validate_result = $validate->scene('login')->check($data);
        if (!$validate_result) {
            return [4003, $validate->getError(), []];
        }

        $appid=config("config.mimi_program")['config']['bd_appid'];
        $secret=config("config.mimi_program")['config']['bd_secret'];
        $js_code=$data['code'];

        $url="https://spapi.baidu.com/oauth/jscode2sessionkey";

        $params=[
            "code"=>$js_code,
            "client_id"=>$appid,
            "sk"=>$secret
        ];

        $result = Curl::post($url,$params);

        $result = json_decode($result,true);

        return [0, 'success', ["code"=>$result["openid"]]];
    }
}